package com.content.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.filter.OncePerRequestFilter;

/**
 * 登录过滤
 */
public class SessionFilter extends OncePerRequestFilter {

	@Override
	protected void doFilterInternal(HttpServletRequest request,
			HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {

		// 过滤的uri
		String[] filter = new String[] {    "/setting", 
				                            "/timelineTpl",
				                            "/commentTpl",
				                            "/writeDown"
				                           
				                            };

		// 请求的uri
		String uri = request.getRequestURI();
        System.out.println("uri: " + uri);
        System.out.println("url: " + request.getRequestURL().toString());
		String obj = (String) request.getSession().getAttribute("id");
		if (obj == null || obj == ""){
			boolean doFilter = false;
			for (String s : filter) {
				if (uri.indexOf(s) != -1) {
					// 如果uri中包含过滤的uri，则不进行过滤
					doFilter = true;
					break;
				}
			}
			
			if (doFilter){
				// 如果session中不存在登录者实体，则弹出框提示重新登录
				// 设置request和response的字符集，防止乱码
				request.setCharacterEncoding("UTF-8");
				response.setCharacterEncoding("UTF-8");
				PrintWriter out = response.getWriter();
				String loginPage = "#/login";
				StringBuilder builder = new StringBuilder();
				builder.append("<script type=\"text/javascript\">");
				builder.append("window.location.href='");
				builder.append(loginPage);
				builder.append("';");
				builder.append("</script>");
				out.print(builder.toString());
			} else{
				doFilter(request, response, filterChain);
			}
			
		} else{
			doFilter(request, response, filterChain);
		}
		
/*		
		
		if (uri.indexOf("writeDown") != -1) {
			// 是否过滤
			boolean doFilter = true;
			for (String s : notFilter) {
				if (uri.indexOf(s) != -1) {
					// 如果uri中包含不过滤的uri，则不进行过滤
					doFilter = false;
					break;
				}
			}
			System.out.println("status: " + doFilter);
			if (doFilter) {
				// 执行过滤
				// 从session中获取登录者实体
				String obj = (String) request.getSession().getAttribute("id");
				System.out.println("session: " + obj);
				if (obj == null || obj == "") {
					// 如果session中不存在登录者实体，则弹出框提示重新登录
					// 设置request和response的字符集，防止乱码
					request.setCharacterEncoding("UTF-8");
					response.setCharacterEncoding("UTF-8");
					PrintWriter out = response.getWriter();
					String loginPage = "#/login";
					StringBuilder builder = new StringBuilder();
					builder.append("<script type=\"text/javascript\">");
					builder.append("window.location.href='");
					builder.append(loginPage);
					builder.append("';");
					builder.append("</script>");
					out.print(builder.toString());
				} else {
					// 如果session中存在登录者实体，则继续
					filterChain.doFilter(request, response);
				}
			} else {
				// 如果不执行过滤，则继续
				filterChain.doFilter(request, response);
			}
		} else {
			// 如果uri中不包含background，则继续
			filterChain.doFilter(request, response);
		}
*/
	}
}
